import pymysql


class mypymysql():
    def __init__(self):
        try:
            self.conn = pymysql.connect(host='localhost', port=3307, user='root', password='usbw', db='top_news',
                                        charset='utf8')
            self.cursor = self.conn.cursor()
        except:
            pass

    def __del__(self):
        try:
            self.cursor.close()
            self.conn.close()
        except:
            pass

    def mydateadd(self, page, name, img, price, url, shop, commit):
        # title = 'title'
        # img_url = 'image_url'
        # url = 'media_url'
        # cursor.execute("INSERT INTO news(name, price, url)VALUES('dsfsd','dsfsdf','jsdfdfsj');")
        self.cursor.execute(
            "INSERT INTO phone(page, name, img,price,url,shop,commit)VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}');".format(
                page, name, img,
                price, url, shop,
                commit))
        self.conn.commit()

    def mydateadd_ctv10(self, location, detailsid, imagelink, playtime, duration, title, album_order_id, targetpage):
        self.cursor.execute(
            "INSERT INTO cctv10position(location, detailsid, imagelink, playtime, duration,title,album_order_id,targetpage)VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');".format(
                location, detailsid, imagelink, playtime, duration, title, album_order_id, targetpage))
        self.conn.commit()

    def mydateadd_Xicidaili(self, ip, time, location, fromurl, date, type):
        self.cursor.execute(
            "INSERT INTO Xicidaili(ip,time,location,fromurl,date,type)VALUES('{0}','{1}','{2}','{3}','{4}','{5}');".format(
                ip,
                time,
                location,
                fromurl,
                date,
                type))
        self.conn.commit()
